package com.esun.cheetah.view.setting;

import java.util.HashMap;

import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.Tools;

public class UserSuggestAct extends Activity implements OnClickListener {
	/** 建议 EditText */
	private EditText etSuggestContent;
	/** 提交 Button */
	private Button btUserCommit;
	/** 返回 Button */
	private Button btBack;
	Dialog inputPwdDialog;
	private boolean ispublish = true;
	private boolean isEmpty = true;
	/** 是不是弹出对话框 */
	private boolean isdialog = true;
	private SubmitSuggest submitSuggest = null;
	Handler suggestHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				ispublish = true;
				etSuggestContent.setText("");
				if (isdialog) {
					createDialog();
				}
				break;
			case Config.COMMUNICATER_EXCEPTION:
			case Config.COMMUNICATER_FAIL:
				ispublish = true;
				if (!Tools.networkStatus(UserSuggestAct.this)) {
					Tools.TipNetworkError(UserSuggestAct.this, Language.INTERNET_FAIL);
					return;
				}
				
				if (submitSuggest != null) {
					String senstiveMsg = submitSuggest.getServerMsg();
					if (senstiveMsg != null && senstiveMsg.equals("您的反馈含有敏感词汇，无法发表")) {
						Tools.TipNetworkError(UserSuggestAct.this, Language.SUBMIT_FAIL);
						return;
					}
					if (senstiveMsg != null && senstiveMsg.equals("参数有误")) {
						Tools.TipNetworkError(UserSuggestAct.this, "发表意见不能为空，请输入!");
						return;
					}
				}
			
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_suggest);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
		initViews();
	}

	private void initViews() {
		etSuggestContent = (EditText) findViewById(R.id.etUserSuggest);
		etSuggestContent.setOnClickListener(this);
		btUserCommit = (Button) findViewById(R.id.btUserSuggest);
		btUserCommit.setOnClickListener(this);
		btBack = (Button) findViewById(R.id.btnUserBack);
		btBack.setOnClickListener(this);
		etSuggestContent.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (count > 0) {
					isEmpty = true;
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	// 提交建议
	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.btUserSuggest:
			String content1= etSuggestContent.getText().toString();
			String content=filterLineFeed(content1);
			Log.e(content, content);
			if (ispublish) {
				if (!Tools.networkStatus(this)) {
					Tools.TipNetworkError(UserSuggestAct.this, Language.INTERNET_FAIL);
					return;
				}
				
				if (UserInfo.getUserInfo().getCk().equals("")) {
					Toast.makeText(UserSuggestAct.this, "请登录后发表意见！", Toast.LENGTH_SHORT).show();
					ispublish = false;
					return;
				}
				if (content.equals("") && isEmpty == true) {
					Toast.makeText(UserSuggestAct.this, "发表意见不能为空，请输入!", Toast.LENGTH_SHORT).show();
					isEmpty = false;
					return;
				}
				submitSuggest = new SubmitSuggest(suggestHandler, content);
				submitSuggest.startCommunicate();
				ispublish = false;
				return;
			}
			if (!ispublish &&!content.equals("")) {
				Toast.makeText(UserSuggestAct.this, "发表太频繁，请稍等一会儿", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btnUserBack:
			finish();
			break;
		default:
			break;
		}
	}

	public class SubmitSuggest extends AbsInferiorityCommunicater {
		private String content;

		public SubmitSuggest(Handler handler, String content) {
			super(handler);
			this.content = content;

		}

		@Override
		public HashMap<String, String> comHashMap() {
			HashMap<String, String> suggestMap = new HashMap<String, String>();
			suggestMap.put(Key.KEY_ID, BizCode.BIZCODE_USER_SUGGEST);
			suggestMap.put("username",
					getSharedPreferences(Key.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE).getString(Key.KEY_USERNAME, ""));
			suggestMap.put("content", content);
			return suggestMap;
		}

		@Override
		public void getJsonContent(String serverRes) throws JSONException {
		}
	}

	/**
	 * 提示对话框
	 */
	public void createDialog() {
		inputPwdDialog = new Dialog(UserSuggestAct.this, R.style.dialog);
		inputPwdDialog.getWindow().setContentView(R.layout.world_enough);
		Button btSure = (Button) inputPwdDialog.getWindow().findViewById(R.id.btUserSure);
		btSure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inputPwdDialog.cancel();
			}
		});
		inputPwdDialog.show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (inputPwdDialog != null && inputPwdDialog.isShowing()) {
			inputPwdDialog.cancel();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		isdialog = false;
	}
	/**
	 * 过滤掉换行
	 * @param content
	 * @return
	 */
	private String filterLineFeed(String content){
		if(content.contains("\n")){
			content=content.replace("\n", "");
		}
		return content;
		
	}
}
